package com.example.sharesinfo.fragment.stock;


import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.sharesinfo.R;
import com.example.sharesinfo.adapter.ScrollablePanelAdapter;
import com.example.sharesinfo.manager.DialogManager;
import com.example.sharesinfo.model.ColumnTitle;
import com.example.sharesinfo.model.DataInfo;
import com.example.sharesinfo.model.HKStockModel;
import com.example.sharesinfo.model.KeChuangModel;
import com.example.sharesinfo.model.StockInfo;
import com.example.sharesinfo.ui.SharesInfoActivity;
import com.example.sharesinfo.utils.LogUtils;
import com.example.sharesinfo.utils.OkHttpUtils;
import com.kelin.scrollablepanel.library.ScrollablePanel;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MarketKeChuangFragment extends Fragment implements RadioGroup.OnCheckedChangeListener {
    private static final String LogTag = "MarketKeChuangFragment:";
    //GetKeChuangList
    private static final int GetKeChuangListSuccess = 1001;
    private static final int GetKeChuangListFailed = 1002;

    private ScrollablePanel scrollablePanel;
    private ScrollablePanelAdapter scrollablePanelAdapter;
    private List<KeChuangModel> mList = new ArrayList<>();

    private RadioGroup rank_type;
    private int type0 = 0;
    private int type1 = 0;
    private int type2 = 0;
    private int type3 = 0;
    private int type4 = 0;
    private int type5 = 0;
    private int type6 = 0;
    private int type7 = 0;
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case GetKeChuangListSuccess:
                    if (mList.size() > 0) {
                        mList.clear();
                    }
                    JSONArray result = (JSONArray) msg.obj;
                    for (int i = 0; i < result.length(); i++) {
                        try {
                            KeChuangModel model = new KeChuangModel();
                            JSONObject object = (JSONObject) result.get(i);
                            model.setCode(object.getString("code"));
                            model.setName(object.getString("name"));
                            model.setNowPrice(object.getString("nowPrice"));
                            model.setDiff_rate(object.getString("diff_rate"));
                            model.setDiff_money(object.getString("diff_money"));
                            model.setTradeAmount(object.getString("tradeAmount"));
                            model.setTurnover(object.getString("turnover"));
                            model.setPe(object.getString("pe"));
                            model.setPb(object.getString("pb"));
                            mList.add(model);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    generateTestData(mList);
                    break;
                case GetKeChuangListFailed:
                    break;
                case 404:
                    break;
            }

            return false;
        }
    });


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_market_ke_chuang, null);
        initView(view);
        initData();
        return view;
    }

    private void initData() {

        List<ColumnTitle> columnTitleList = new ArrayList<>();

        String column1 = "当前价";
        String column2 = "涨跌幅";
        String column3 = "涨跌额";
        String column4 = "成交量";
        String column5 = "换手率";
        String column6 = "市盈率";
        String column7 = "市净率";
        String column8 = "市净率";
        ColumnTitle columnTitle1 = new ColumnTitle();
        columnTitle1.setColumnTitle(column1);
        columnTitleList.add(columnTitle1);
        ColumnTitle columnTitle2 = new ColumnTitle();
        columnTitle2.setColumnTitle(column2);
        columnTitleList.add(columnTitle2);
        ColumnTitle columnTitle3 = new ColumnTitle();
        columnTitle3.setColumnTitle(column3);
        columnTitleList.add(columnTitle3);
        ColumnTitle columnTitle4 = new ColumnTitle();
        columnTitle4.setColumnTitle(column4);
        columnTitleList.add(columnTitle4);
        ColumnTitle columnTitle5 = new ColumnTitle();
        columnTitle5.setColumnTitle(column5);
        columnTitleList.add(columnTitle5);
        ColumnTitle columnTitle6 = new ColumnTitle();
        columnTitle6.setColumnTitle(column6);
        columnTitleList.add(columnTitle6);
        ColumnTitle columnTitle7 = new ColumnTitle();
        columnTitle7.setColumnTitle(column7);
        columnTitleList.add(columnTitle7);
        ColumnTitle columnTitle8 = new ColumnTitle();
        columnTitle7.setColumnTitle(column8);
        columnTitleList.add(columnTitle8);
        scrollablePanelAdapter.setColumnTitleList(columnTitleList);

    }

    private void initView(View view) {
        scrollablePanel = view.findViewById(R.id.ke_chuang_stocks_scrollable_panel);
        scrollablePanelAdapter = new ScrollablePanelAdapter(0, new ScrollablePanelAdapter.OnBindDataListener() {
            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, final int row, int column) {
                int viewType = scrollablePanelAdapter.getItemViewType(row, column);
                switch (viewType) {
                    case ScrollablePanelAdapter.COLUMN_TITLE_TYPE:
                        scrollablePanelAdapter.setColumnTitleView(column, (ScrollablePanelAdapter.ColumnTitleViewHolder) holder);
                        break;
                    case ScrollablePanelAdapter.STOCK_TYPE:
                        scrollablePanelAdapter.setStockView(row, (ScrollablePanelAdapter.StockViewHolder) holder);
                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //Toast.makeText(getContext(),  mList.get(row - 1).getCode(), Toast.LENGTH_SHORT).show();
                                SharesInfoActivity.startActivity(getContext(), mList.get(row - 1).getCode());
                            }
                        });
                        break;
                    case ScrollablePanelAdapter.DATA_TYPE:
                        scrollablePanelAdapter.setDataView(row, column, (ScrollablePanelAdapter.DataViewHolder) holder);
                        break;
                    case ScrollablePanelAdapter.TITLE_TYPE:
                        break;
                }

            }
        });
        scrollablePanel.setPanelAdapter(scrollablePanelAdapter);
        rank_type = view.findViewById(R.id.ke_chuang_stocks_ranking_radio_group);
        rank_type.setOnCheckedChangeListener(this);
        getKeChuangList(1, "diff_rate", "-1");
    }

    private void getKeChuangList(int page, String sortField, String sortType) {
        String url = "http://119.23.221.142/api/stock/GetKeChuangList?Page=" + page +
                "&SortField=" + sortField + "&SortType=" + sortType;
        OkHttpUtils.getInstance().Get(url, new OkHttpUtils.okHttpListener() {
            Message message = mHandler.obtainMessage();

            @Override
            public void OnSuccess(String s, int code) throws JSONException {
                LogUtils.i("MarketKeChuangFragment:" + s);
                JSONObject object = new JSONObject(s);
                if (object.getString("code").equals("200")) {
                    message.what = GetKeChuangListSuccess;
                    message.obj = object.getJSONArray("data");
                    mHandler.sendMessage(message);
                } else {
                    message.what = GetKeChuangListFailed;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void OnFailed(String s) {
                message.what = 404;
                message.obj = s;
                mHandler.sendMessage(message);
            }
        });
    }

    private void generateTestData(List<KeChuangModel> list) {
        List<StockInfo> stockInfoList = new ArrayList<>();
        List<List<DataInfo>> dataList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            KeChuangModel model = list.get(i);
            StockInfo stockInfo = new StockInfo();
            stockInfo.setStockName(model.getName());
            stockInfo.setRoomId(i);
            stockInfo.setStockId(model.getCode());
            stockInfoList.add(stockInfo);
            addData(model, dataList);
        }
        scrollablePanelAdapter.setStockInfoList(stockInfoList);
        scrollablePanelAdapter.setDataList(dataList);
        scrollablePanel.notifyDataSetChanged();
    }

    private void addData(KeChuangModel model, List<List<DataInfo>> dataList) {
        List<DataInfo> dataInfoList = new ArrayList<>();
        DataInfo dataInfo1 = new DataInfo();
        dataInfo1.setData(model.getNowPrice());
        dataInfoList.add(dataInfo1);

        DataInfo dataInfo2 = new DataInfo();
        dataInfo2.setData(model.getDiff_rate());
        dataInfoList.add(dataInfo2);

        DataInfo dataInfo3 = new DataInfo();
        dataInfo3.setData(model.getDiff_money());
        dataInfoList.add(dataInfo3);

        DataInfo dataInfo4 = new DataInfo();
        dataInfo4.setData(model.getTradeAmount());
        dataInfoList.add(dataInfo4);

        DataInfo dataInfo5 = new DataInfo();
        dataInfo5.setData(model.getTurnover());
        dataInfoList.add(dataInfo5);

        DataInfo dataInfo6 = new DataInfo();
        dataInfo6.setData(model.getPe());
        dataInfoList.add(dataInfo6);

        DataInfo dataInfo7 = new DataInfo();
        dataInfo7.setData(model.getPb());
        dataInfoList.add(dataInfo7);

        dataList.add(dataInfoList);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.ke_chuang_stocks_ranking_rb_1:
                if (type0 == 0) {
                    getKeChuangList(1, "diff_rate", "-1");
                    type0 = 1;
                    type1 = 0;
                    type2 = 0;
                    type3 = 0;
                    type4 = 0;
                    type5 = 0;
                    type6 = 0;
                    type7 = 0;
                }
                break;
            case R.id.ke_chuang_stocks_ranking_rb_2:
                if (type1 == 0) {
                    getKeChuangList(1, "diff_rate", "1");
                    type0 = 0;
                    type1 = 1;
                    type2 = 0;
                    type3 = 0;
                    type4 = 0;
                    type5 = 0;
                    type6 = 0;
                    type7 = 0;
                }
                break;
            case R.id.ke_chuang_stocks_ranking_rb_3:
                if (type2 == 0) {
                    getKeChuangList(1, "nowPrice", "-1");
                    type0 = 0;
                    type1 = 0;
                    type2 = 1;
                    type3 = 0;
                    type4 = 0;
                    type5 = 0;
                    type6 = 0;
                    type7 = 0;
                }
                break;
            case R.id.ke_chuang_stocks_ranking_rb_4:
                if (type3 == 0) {
                    getKeChuangList(1, "diff_money", "-1");
                    type0 = 0;
                    type1 = 0;
                    type2 = 0;
                    type3 = 1;
                    type4 = 0;
                    type5 = 0;
                    type6 = 0;
                    type7 = 0;
                }
                break;
            case R.id.ke_chuang_stocks_ranking_rb_5:
                if (type4 == 0) {
                    getKeChuangList(1, "tradeAmount", "-1");
                    type0 = 0;
                    type1 = 0;
                    type2 = 0;
                    type3 = 0;
                    type4 = 1;
                    type5 = 0;
                    type6 = 0;
                    type7 = 0;
                }
                break;
            case R.id.ke_chuang_stocks_ranking_rb_6:
                if (type5 == 0) {
                    getKeChuangList(1, "turnover", "-1");
                    type0 = 0;
                    type1 = 0;
                    type2 = 0;
                    type3 = 0;
                    type4 = 0;
                    type5 = 1;
                    type6 = 0;
                    type7 = 0;
                }
                break;
            case R.id.ke_chuang_stocks_ranking_rb_7:
                if (type6 == 0) {
                    getKeChuangList(1, "pe", "-1");
                    type0 = 0;
                    type1 = 0;
                    type2 = 0;
                    type3 = 0;
                    type4 = 0;
                    type5 = 0;
                    type6 = 1;
                    type7 = 0;
                }
                break;
            case R.id.ke_chuang_stocks_ranking_rb_8:
                if (type7 == 0) {
                    getKeChuangList(1, "pb", "-1");
                    type0 = 0;
                    type1 = 0;
                    type2 = 0;
                    type3 = 0;
                    type4 = 0;
                    type5 = 0;
                    type6 = 0;
                    type7 = 1;
                }
                break;
        }
    }
}
